package com.bean.per;

import java.io.UnsupportedEncodingException;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;

import com.bean.PubBean;
import com.datasource.DSManager;
import com.datasource.DSSession;
import com.datasource.SQLQuery;
import com.datasource.SQLUpdater;
import com.module.MyDataTable;
import com.util.DateHelper;
import com.util.MD5;

public class PerBean extends PubBean{
	private String	name;
	private String	id;
	private String	role_id;
	private String	org_id;
	private String  org_name;

	
	public int getUsersCount() throws SQLException
	{
		SQLQuery query = DSManager.getSQLQuery();
		StringBuffer sql = new StringBuffer("select count(*) from ( "+
				"select r.role_name,r.role_id,p.per_id  from t_base_role r,t_per_role p where r.role_id=p.s_id) r " +
				"right join  "+
				"(select p.id,p.name,o.org_name from t_personnel_information p ,t_evaluationorg o "+
				"where p.org_id=o.id) p "+
				"on r.per_id = p.id");
			if(null != name && !"".equals(name)){
			sql.append(" and p.name like '%"+name+"%' ");
		}
		if(null != org_name && !"".equals(org_name)){
			sql.append(" and p.org_name like '%"+org_name+"%' ");
		}
		return query.getIntResult( sql.toString() );
	}
	
	public MyDataTable getUsers( int pageNumber, int pageSize ) throws SQLException
	{
		SQLQuery query = DSManager.getSQLQuery();
		StringBuffer sql = new StringBuffer("select p.id,p.name,p.org_name,role_id from ( "+
			"select r.role_name,r.role_id,p.per_id  from t_base_role r,t_per_role p where r.role_id=p.s_id) r " +
			"right join   "+
			"(select p.id,p.name,o.org_name,p.org_id from t_personnel_information p ,t_evaluationorg o "+
			"where p.org_id=o.id) p "+
			"on r.per_id = p.id");
		if(null != name && !"".equals(name)){
			sql.append(" and p.name like '%"+name+"%' ");
		}
		if(null != org_name && !"".equals(org_name)){
			sql.append(" and p.org_name like '%"+org_name+"%' ");
		}
		sql.append(" order by p.org_id");
		return query.DataTablePageQuery( sql.toString(), pageNumber, pageSize );
	}
	
	public boolean update(PerBean p) throws SQLException{
		if(null!=p){
			return p.update();
		}else{
			return false;
		}
	}
	public boolean update() throws SQLException
	{
		SQLUpdater updater = DSManager.getSQLUpdater();
		StringBuffer sql = new StringBuffer("delete from t_per_role where per_id=? ");
		Object [] prepareParam = new Object[1];
		prepareParam[0] = id ;
			
		StringBuffer sql2 = new StringBuffer("insert into t_per_role (s_id,per_id) values (?,?) ");
		Object [] prepareParam2 = new Object[2];
		prepareParam2[0] = role_id ;
		prepareParam2[1] = id ;
		if( updater.executePrepareUpdate( sql.toString(), prepareParam) >= 0 
				&& updater.executePrepareUpdate( sql2.toString(), prepareParam2) > 0 )
		{
			return true;
		}
		return false;
	}
	
	public List getSelectRoles() throws SQLException, UnsupportedEncodingException
	{
		StringBuffer sql = new StringBuffer("SELECT role_id as code_value,role_name as code_name FROM t_base_role");
		SQLQuery query = DSManager.getSQLQuery();
		List list = query.ListQuery( sql.toString() );
		return list;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getRole_id() {
		return role_id;
	}

	public void setRole_id(String role_id) {
		this.role_id = role_id;
	}

	public String getOrg_id() {
		return org_id;
	}

	public void setOrg_id(String org_id) {
		this.org_id = org_id;
	}

	public String getOrg_name() {
		return org_name;
	}

	public void setOrg_name(String org_name) {
		this.org_name = org_name;
	}
	
	

}
